import logging

import pytest
import requests

from api.open_account_api import OpenAccountApi
from api.reg_login_api import RegLoginApi
from common.util import read_xlsx, html_parser, build_account, common_assert
from config import path_cfg


@pytest.fixture(params=read_xlsx('P2P金融项目-接口测试用例.xlsx', '开户模块', 2, 4),
                ids=['实名认证成功（必填项正确）', '实名认证失败（姓名为空）', '实名认证失败（身份证为空）'])
def real_name_data(request):
    return request.param


class TestOpenAccount(object):

    @pytest.fixture(autouse=True)
    def setup_func(self):
        self.session = requests.Session()
        self.reg_Login_api = RegLoginApi(self.session)
        self.open_account_api = OpenAccountApi(self.session)

        phone = path_cfg.REG_LOGIN_PHONE
        build_account('open_user', self.session, phone)

        login_req_body = {
            "keywords": phone,
            "password": "a123456"
        }
        self.reg_Login_api.user_login(login_req_body)
        yield
        self.session.close()

    # 实名认证测试用例
    @pytest.mark.run(order=1)
    # @pytest.mark.parametrize('title, req_body, expect', read_xlsx('P2P金融项目-接口测试用例.xlsx', '开户模块', 2, 4))
    def test_real_name(self, real_name_data):
        print(real_name_data)
        resp = self.open_account_api.real_name(real_name_data[1])

        common_assert(real_name_data[2], resp)

    # 开户
    @pytest.mark.run(order=2)
    def test_open_account(self):
        resp = self.open_account_api.open_account()

    # 第三方开户测试用例
    @pytest.mark.run(order=3)
    def test_third_open_account(self):
        resp = self.open_account_api.open_account()

        form_data = resp.json().get('description').get('form')

        parser_data = html_parser(form_data)
        logging.info(parser_data)

        resp = self.open_account_api.third_open_account(parser_data.get('url'), parser_data.get('req_body'))
        assert 'UserRegister OK' == resp.text
